package org.example.petadoption.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.petadoption.entity.Orders;
import org.example.petadoption.entity.ResultBody;
import org.example.petadoption.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author PetAdoption
 * @since 2025-10-20
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    IOrdersService ordersService;

    //查询所有订单
    @GetMapping("allOrders")
    public ResultBody allOrders() {
        List<Orders> ordersList = ordersService.list();
        return ResultBody.success("所有订单", ordersList);
    }

    //查询用户订单
    @PostMapping("/selectOrders")
    public ResultBody selectOrders(int id) {
        if (id <= 0) {
            return ResultBody.fail("用户ID不合法");
        }
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, id);
        List<Orders> ordersList = ordersService.list(queryWrapper);
        return ResultBody.success("ID " + id + " 的订单" , ordersList);
    }


}
